/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.dao.UserDAO;
import model.dao.services.UserDAOServices;
import model.entities.User;

/**
 *
 * @author Ku81
 */
public class LoginServlet extends HttpServlet {
    private final UserDAOServices USER_DAO = UserDAO.getInstance();
    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
//        processRequest(request, response);
        doLogin(request, response);
    }
    
    
    //CHECK LOGIN
    protected void doLogin(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException{
        
//        response.setContentType("text/html;charset=UTF-8");
        
        String userName = (String) request.getParameter("userName");
        String password = (String) request.getParameter("password");

        if(USER_DAO.checkLogin(userName, password)){
            System.out.println("LOGIN OK");
            User userSession = USER_DAO.getByUserName(userName);
            HttpSession session = request.getSession();
            session.setAttribute("userSession", userSession);
            session.setMaxInactiveInterval(60*60);
            response.sendRedirect("index.jsp");
        }else{
            System.out.println("LOGIN FALIED");
            response.sendRedirect("index.jsp#loginForm");
        }
    }
    
    protected void doLogout(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException{
        HttpSession session = request.getSession();
        session.invalidate();
        session.removeAttribute("userSession");
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
